$(function() {
	refresh();
	$("input[name='beginDateType']").change(function() {
		refresh();
	});
	$("#report1Container").panel({
		onResize : resizeEchartReport
	});
	resizeEchartReport();
});

var hasUserSelect = false;
function userSelect(){
	hasUserSelect = true;
}

function changeBeginDate() {
	if (!hasUserSelect) return;
	hasUserSelect = false;
	$(":radio[name='beginDateType']").prop("checked", false);
	refresh();
}

function changeAcount(newValue, oldValue) {
	if (oldValue && newValue) {
		refresh();
	}
}

function changeStatusDate() {
	if (!hasUserSelect) return;
	hasUserSelect = false;
	refreshStatus();
}

function refresh(forceRefresh) {
	var acountId = $("#acountId").combobox("getValue");
	var data = $("#requestForm").form("getData");
	data.summaryDate = $("#summaryDate").datebox("getValue");
	data.forceRefresh = forceRefresh;
	data.acountId = acountId;
	$.ajax("./asset.json", {
		mask : true,
		data : data,
		success : loadData
	});
}

function refreshStatus(summaryDateType) {
	var acountId = $("#acountId").combobox("getValue");
	var summaryDate = $("#summaryDate").datebox("getValue");
	$.ajax("./getstatus.json", {
		mask : true,
		data : {
			summaryDate : summaryDate,
			summaryDateType : summaryDateType,
			acountId : acountId
		},
		success : loadData
	});
}

function loadData(data){
	if(data.status){
		loadStatus(data.status);
	} 
	if(data.chart){
		loadChart(data.chart);
	}
}

function loadStatus(data){
	$("#acountStatus").datagrid("loadData", data.positions);
	$("#acountStatusForm").form("load", data.summary);
	if(data.bills && data.bills.length>0){
		$("#bill").datagrid("getPanel").show();
		$("#bill").datagrid("loadData",data.bills);
	} else {
		$("#bill").datagrid("getPanel").hide();
		$("#bill").datagrid("loadData",[]);
	}
}

var lastWidth = 0;
function resizeEchartReport() {
	var containerWidth = $("#report1Container").width();
	var width = containerWidth;
	if(containerWidth>1000){
		width = 1000;
	}else if(containerWidth<600){
		width = 600;
	}
	if(lastWidth == width){
		return;
	}
	var height = width/2;
	$("#report1").width(width);
	$("#report1").height(height);
	var myChart1 = echarts.getInstanceByDom(document.getElementById('report1'));
	if(myChart1) myChart1.resize();
	
	$("#report2").width(width);
	$("#report2").height(height);
	var myChart2 = echarts.getInstanceByDom(document.getElementById('report2'));
	if(myChart2) myChart2.resize();
};

function loadChart(data) {
	$("#requestForm").form("load", data.requestData);
	// 基于准备好的dom，初始化echarts实例
	var myChart = echarts.init(document.getElementById('report1'));
	// 指定图表的配置项和数据
	var option = {
		tooltip : {
			trigger : 'axis',
			axisPointer : {
				type : 'cross'
			},
			formatter : function(params) {
				var res = params[0].name;
				for (var i = 0, l = params.length; i < l; i++) {
					var a = '<span style="display:inline-block;margin-right:5px;border-radius:10px;width:9px;height:9px;background-color:' + params[i].color + '"></span>';
					res += '<br/>' + a + params[i].seriesName + ' : ' + params[i].value.toFixed(2) + "%";
				}
				return res;
			}
		},
		legend : {
			data : [ data.baseIndexName, '账户' ]
		},
		dataZoom : {
			show : true,
			realtime : true,
			start : 0,
			end : 100
		},
		xAxis : {
			type : "category",
			data : data.timeLine
		},
		yAxis : [ {
			name : '收益%',
			type : 'value',
			axisLabel : {
				formatter : function(value, index) {
					return value+"%";
				}
			}
		} ],
		series : [ {
			name : data.baseIndexName,
			type : 'line',
			data : data.baseProfit
		}, {
			name : '账户',
			type : 'line',
			data : data.profit
		} ]
	};
	myChart.setOption(option);

	var myChart = echarts.init(document.getElementById('report2'));
	// 指定图表的配置项和数据
	var option = {
		tooltip : {
			trigger : 'axis',
			axisPointer : {
				type : 'cross'
			},
			formatter : function(params) {
				var res = params[0].name;
				for (var i = 0, l = params.length; i < l; i++) {
					var a = '<span style="display:inline-block;margin-right:5px;border-radius:10px;width:9px;height:9px;background-color:' + params[i].color + '"></span>';
					res += '<br/>' + a + params[i].seriesName + ' : ' + params[i].value.toFixed(0);
				}
				return res;
			}
		},
		legend : {
			data : [ '净资产', '持仓', '收益' ]
		},
		dataZoom : {
			show : true,
			realtime : true,
			start : 0,
			end : 100
		},
		xAxis : {
			type : "category",
			data : data.timeLine
		},
		yAxis : [ {
			name : '资产(元)',
			type : 'value',
		} ],
		series : [ {
			name : '净资产',
			type : 'line',
			data : data.asset,
			areaStyle : {
				normal : {}
			},
		}, {
			name : '持仓',
			type : 'line',
			data : data.marketValue,
			areaStyle : {
				normal : {}
			}
		}, {
			name : '收益',
			type : 'line',
			data : data.profitMoney,
			areaStyle : {
				normal : {}
			},
		} ]
	};
	myChart.setOption(option);
}

function profitStyle(value,row,index){
	if (value > 0){  
		return 'color:red;';
	} else if (value < 0){
		return 'color:green;';
	}
}

function posRatioFormatter(value,rowData,rowIndex){
	var ratio = new Number(value).toFixed(2);
	var progress = $("<span><div></div></span>");
	progress.find("div").progressbar({value:ratio,width:70});
	return progress.html();
}
